#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
生成拼音组合音频文件的脚本
生成声母+韵母的组合拼音音频
需要安装: pip install gtts
"""

from gtts import gTTS
import os
import time

# 创建 sounds/pinyin 目录
pinyin_dir = 'sounds/pinyin'
os.makedirs(pinyin_dir, exist_ok=True)

# 拼音组合数据（与 challenge.js 中的一致）
# 添加一声拼音（tone1）用于生成音频
pinyin_combinations = [
    # 简单拼音（单韵母）
    {"pinyin": "ba", "chinese": "爸", "tone1": "bā"},
    {"pinyin": "ma", "chinese": "妈", "tone1": "mā"},
    {"pinyin": "fa", "chinese": "发", "tone1": "fā"},
    {"pinyin": "da", "chinese": "大", "tone1": "dā"},
    {"pinyin": "ta", "chinese": "他", "tone1": "tā"},
    {"pinyin": "na", "chinese": "那", "tone1": "nā"},
    {"pinyin": "la", "chinese": "拉", "tone1": "lā"},
    {"pinyin": "ga", "chinese": "嘎", "tone1": "gā"},
    {"pinyin": "ka", "chinese": "卡", "tone1": "kā"},
    {"pinyin": "ha", "chinese": "哈", "tone1": "hā"},
    {"pinyin": "zha", "chinese": "扎", "tone1": "zhā"},
    {"pinyin": "cha", "chinese": "查", "tone1": "chā"},
    {"pinyin": "sha", "chinese": "沙", "tone1": "shā"},
    {"pinyin": "za", "chinese": "杂", "tone1": "zā"},
    {"pinyin": "ca", "chinese": "擦", "tone1": "cā"},
    {"pinyin": "sa", "chinese": "撒", "tone1": "sā"},
    
    # 复韵母（所有拼音组合都使用一声）
    {"pinyin": "bai", "chinese": "白", "tone1": "bāi"},
    {"pinyin": "pai", "chinese": "拍", "tone1": "pāi"},
    {"pinyin": "mai", "chinese": "买", "tone1": "māi"},
    {"pinyin": "dai", "chinese": "带", "tone1": "dāi"},
    {"pinyin": "tai", "chinese": "太", "tone1": "tāi"},
    {"pinyin": "nai", "chinese": "奶", "tone1": "nāi"},
    {"pinyin": "lai", "chinese": "来", "tone1": "lāi"},
    {"pinyin": "gai", "chinese": "改", "tone1": "gāi"},
    {"pinyin": "kai", "chinese": "开", "tone1": "kāi"},
    {"pinyin": "hai", "chinese": "海", "tone1": "hāi"},
    
    {"pinyin": "bao", "chinese": "包", "tone1": "bāo"},
    {"pinyin": "pao", "chinese": "跑", "tone1": "pāo"},
    {"pinyin": "mao", "chinese": "猫", "tone1": "māo"},
    {"pinyin": "dao", "chinese": "到", "tone1": "dāo"},
    {"pinyin": "tao", "chinese": "桃", "tone1": "tāo"},
    {"pinyin": "nao", "chinese": "脑", "tone1": "nāo"},
    {"pinyin": "lao", "chinese": "老", "tone1": "lāo"},
    {"pinyin": "gao", "chinese": "高", "tone1": "gāo"},
    {"pinyin": "kao", "chinese": "考", "tone1": "kāo"},
    {"pinyin": "hao", "chinese": "好", "tone1": "hāo"},
    
    {"pinyin": "ban", "chinese": "班", "tone1": "bān"},
    {"pinyin": "pan", "chinese": "盘", "tone1": "pān"},
    {"pinyin": "man", "chinese": "满", "tone1": "mān"},
    {"pinyin": "dan", "chinese": "单", "tone1": "dān"},
    {"pinyin": "tan", "chinese": "谈", "tone1": "tān"},
    {"pinyin": "nan", "chinese": "男", "tone1": "nān"},
    {"pinyin": "lan", "chinese": "蓝", "tone1": "lān"},
    {"pinyin": "gan", "chinese": "干", "tone1": "gān"},
    {"pinyin": "kan", "chinese": "看", "tone1": "kān"},
    {"pinyin": "han", "chinese": "汉", "tone1": "hān"},
    
    {"pinyin": "bang", "chinese": "帮", "tone1": "bāng"},
    {"pinyin": "pang", "chinese": "旁", "tone1": "pāng"},
    {"pinyin": "mang", "chinese": "忙", "tone1": "māng"},
    {"pinyin": "dang", "chinese": "当", "tone1": "dāng"},
    {"pinyin": "tang", "chinese": "糖", "tone1": "tāng"},
    {"pinyin": "nang", "chinese": "囊", "tone1": "nāng"},
    {"pinyin": "lang", "chinese": "狼", "tone1": "lāng"},
    {"pinyin": "gang", "chinese": "刚", "tone1": "gāng"},
    {"pinyin": "kang", "chinese": "康", "tone1": "kāng"},
    {"pinyin": "hang", "chinese": "航", "tone1": "hāng"}
]

def generate_audio(tone1_text, filename, pinyin_text=""):
    """生成单个拼音组合音频
    使用带一声声调符号的拼音来生成音频（如 bā, mā）
    """
    try:
        # 使用带一声声调符号的拼音生成音频
        # 这样 gTTS 会读出拼音的一声，而不是汉字的实际声调
        tts = gTTS(text=tone1_text, lang='zh', slow=False)
        filepath = os.path.join(pinyin_dir, filename)
        tts.save(filepath)
        print(f"✓ 生成: {filename} ({pinyin_text} -> {tone1_text})")
        return True
    except Exception as e:
        print(f"✗ 失败: {filename} ({pinyin_text} -> {tone1_text}) - {e}")
        return False

print("=" * 60)
print("拼音组合音频生成工具")
print("=" * 60)
print(f"\n开始生成 {len(pinyin_combinations)} 个拼音组合音频文件...\n")

total_count = 0
success_count = 0

for item in pinyin_combinations:
    total_count += 1
    filename = f"{item['pinyin']}.mp3"
    
    # 如果文件已存在，跳过
    if os.path.exists(os.path.join(pinyin_dir, filename)):
        print(f"  ✓ {filename} ({item['pinyin']}) - 已存在，跳过")
        success_count += 1
        continue
    
    # 使用带一声声调符号的拼音（tone1）来生成音频
    tone1 = item.get('tone1', item['pinyin'])
    if generate_audio(tone1, filename, item['pinyin']):
        success_count += 1
    time.sleep(0.5)  # 避免请求过快

print("\n" + "=" * 60)
print(f"完成！成功: {success_count}/{total_count}")
print(f"音频文件保存在: {pinyin_dir}/")
print("=" * 60)

